<template>
  <div class="main_box" style="padding:10px;">
    <div class="add_wrap">
      <div class="tit">
        <h2>基础信息</h2>
        <el-button plain size="small" type="success" icon="fa fa-mail-forward" @click="returnList"> 返 回</el-button>
      </div>
      <el-form :model="specimenForm" ref="specimenForm" label-width="200px" :rules="roleRules">
        <el-row>
          <el-form-item label="样本名称" prop="insSampleName">
            <el-input v-model="specimenForm.insSampleName"></el-input>
          </el-form-item>
          <el-form-item label="样本编号" prop="insSampleCode">
            <el-input v-model="specimenForm.insSampleCode"></el-input>
          </el-form-item>
        </el-row>

        <div class="tit">
          <h2>医检机构</h2>
        </div>
        <el-row>
          <el-form-item label="医检机构" prop="institutionId">
            <el-select v-model="specimenForm.institutionId" filterable clearable reserve-keyword :loading="loadingIns"
              placeholder="请选择医检机构" >
              <el-option v-for="item in pickInstitutionList" :key="item.value" :label="item.label" :value="item.value">
              </el-option>
            </el-select>
          </el-form-item>

        </el-row>

        <el-row>
          <el-form-item>
            <el-button type="primary" @click="sureReportClick">保 存</el-button>
            <el-button @click="returnList">取 消</el-button>
          </el-form-item>
        </el-row>
      </el-form>
    </div>
  </div>
</template>

<script>
import * as message from '@/constants/message';
import medicalApi from "@/api/medicalspecimen/index";
import InstitutionApi from "@/api/institution/institution";
export default {
  data() {
    return {
      loadingIns: false,
      loadingCla: false,
      pickInstitutionList: [],
      disabled: false,
      isCenter: true,
      specimenForm: {
        insSampleName: '',
        insSampleCode: '',
        insSampleId: this.$route.query.id || '',
        institutionId: ''
      },
      roleRules: {
        insSampleName: [
          {
            required: true,
            message: '请输入样本名称',
            trigger: 'blur'
          }
        ],
        insSampleCode: [
          {
            required: true,
            message: '请输入样本编号',
            trigger: 'blur'
          }
        ],
        institutionId: [
          {
            required: true,
            message: '请选择医检机构',
            trigger: 'change'
          }
        ]
      }
    }
  },
  components: {
  },
  methods: {
    init() {
      this.listInstitution()
      if (this.$route.query.id) {
        this.getData()
      }
    },
    listInstitution() {
      let _this = this
      _this.loadingIns = true
      let cond = {
        institutionType: 1,
      }
      InstitutionApi.getInstitutionNameList(cond)
        .then(res => {
          setTimeout(() => {
            _this.loadingIns = false
            _this.pickInstitutionList = res.data.map(item => {
              return {
                value: item.institutionId,
                label: item.institutionName
              }
            })
          }, 200)
        })
        .catch(error => {
          _this.pickInstitutionList = []
          console.error(error)
        })
    },
    sureReportClick() {
      this.$refs.specimenForm.validate(valid => {
        if (valid) {
          this.$confirm(
            message.MSG_CONFIRM_SAVE_ALTER,
            message.TITLE_PROMPT,
            {}
          ).then(() => {
            this.alterReport()
          })
        }
      })
    },
    alterReport() {
      let _this = this
      let specimenFormData = {
        insSampleName: _this.specimenForm.insSampleName,
        insSampleCode: _this.specimenForm.insSampleCode,
        institutionId: _this.specimenForm.institutionId,
      }
      if(_this.specimenForm.insSampleId){
        specimenFormData.insSampleId= _this.specimenForm.insSampleId
        medicalApi.editSpecimen(specimenFormData)
        .then(data => {
          this.$Utils.notifySuccess()
          this.returnList();
        })
      }else{
        medicalApi.addSpecimen(specimenFormData)
        .then(data => {
          this.$Utils.notifySuccess()
          this.returnList();
        })
      }
    },
    getData() {
      medicalApi.getDetail({
        insSampleId: this.$route.query.id
      }).then(res => {
        this.$Utils.bindingBean(this.specimenForm, res.data)
        if (res.data.institutionId) {
          this.specimenForm.institutionId = parseInt(res.data.institutionId)
        }
      })
    },
    returnList() {
      this.$store.dispatch('delView', this.$route);
      this.$router.go(-1)
    }
  },
  mounted() {
    this.init()
  }
}
</script>
<style lang="scss" src="@/assets/css/views/add.scss" scope></style>
